﻿//
// MudTools.OfficeInterop 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

namespace MudTools.OfficeInterop.Excel.Imps;
/// <summary>
/// Excel TextFrame 对象的二次封装实现类
/// </summary>
internal class ExcelTextFrame : IExcelTextFrame
{
    private MsExcel.TextFrame _textFrame;
    private bool _disposedValue;

    internal ExcelTextFrame(MsExcel.TextFrame textFrame)
    {
        _textFrame = textFrame;
        _disposedValue = false;
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposedValue) return;

        if (disposing)
        {
            if (_textFrame != null)
                Marshal.ReleaseComObject(_textFrame);
            _textFrame = null;
        }

        _disposedValue = true;
    }

    public void Dispose() => Dispose(true);

    public MsoTextOrientation Orientation
    {
        get => _textFrame != null ? _textFrame.Orientation.EnumConvert(MsoTextOrientation.msoTextOrientationMixed) : MsoTextOrientation.msoTextOrientationMixed;
        set
        {
            if (_textFrame != null)
                _textFrame.Orientation = value.EnumConvert(MsCore.MsoTextOrientation.msoTextOrientationMixed);
        }
    }

    public bool AutoMargins
    {
        get => _textFrame != null && _textFrame.AutoMargins;
        set { if (_textFrame != null) _textFrame.AutoMargins = value; }
    }

    public bool AutoSize
    {
        get => _textFrame != null && _textFrame.AutoSize;
        set { if (_textFrame != null) _textFrame.AutoSize = value; }
    }


    public int ReadingOrder
    {
        get => _textFrame != null ? _textFrame.ReadingOrder : 0;
        set
        {
            if (_textFrame != null)
                _textFrame.ReadingOrder = value;
        }
    }

    public XlHAlign HorizontalAlignment
    {
        get => _textFrame != null ? _textFrame.HorizontalAlignment.EnumConvert(XlHAlign.xlHAlignCenter) : XlHAlign.xlHAlignCenter;
        set
        {
            if (_textFrame != null)
                _textFrame.HorizontalAlignment = value.EnumConvert(MsExcel.XlHAlign.xlHAlignCenter);
        }
    }

    public XlVAlign VerticalAlignment
    {
        get => _textFrame != null ? _textFrame.VerticalAlignment.EnumConvert(XlVAlign.xlVAlignCenter) : XlVAlign.xlVAlignCenter;
        set
        {
            if (_textFrame != null)
                _textFrame.VerticalAlignment = value.EnumConvert(MsExcel.XlVAlign.xlVAlignCenter);
        }
    }

    public XlOartVerticalOverflow VerticalOverflow
    {
        get => _textFrame != null ? _textFrame.VerticalOverflow.EnumConvert(XlOartVerticalOverflow.xlOartVerticalOverflowClip) : XlOartVerticalOverflow.xlOartVerticalOverflowClip;
        set
        {
            if (_textFrame != null)
                _textFrame.VerticalOverflow = value.EnumConvert(MsExcel.XlOartVerticalOverflow.xlOartVerticalOverflowClip);
        }

    }

    public XlOartHorizontalOverflow HorizontalOverflow
    {
        get => _textFrame != null ? _textFrame.HorizontalOverflow.EnumConvert(XlOartHorizontalOverflow.xlOartHorizontalOverflowClip) : XlOartHorizontalOverflow.xlOartHorizontalOverflowClip;
        set
        {
            if (_textFrame != null)
                _textFrame.HorizontalOverflow = value.EnumConvert(MsExcel.XlOartHorizontalOverflow.xlOartHorizontalOverflowClip);
        }
    }

    public float MarginLeft
    {
        get => _textFrame?.MarginLeft ?? 0;
        set { if (_textFrame != null) _textFrame.MarginLeft = value; }
    }

    public float MarginRight
    {
        get => _textFrame?.MarginRight ?? 0;
        set { if (_textFrame != null) _textFrame.MarginRight = value; }
    }

    public float MarginTop
    {
        get => _textFrame?.MarginTop ?? 0;
        set { if (_textFrame != null) _textFrame.MarginTop = value; }
    }

    public float MarginBottom
    {
        get => _textFrame?.MarginBottom ?? 0;
        set { if (_textFrame != null) _textFrame.MarginBottom = value; }
    }

    public IExcelCharacters? Characters(int? start = null, int? length = null)
    {
        var charactersObj = _textFrame?.Characters(start.ComArgsVal(), length.ComArgsVal());
        MsExcel.Range? range = null;
        if (_textFrame?.Parent is MsExcel.Range r1)
            range = r1;
        if (_textFrame?.Parent is MsExcel.Shape shape)
            if (shape.Parent is MsExcel.Range r2)
                range = r2;
        if (_textFrame?.Parent is MsExcel.ShapeRange shapeRange)
            if (shapeRange.Parent is MsExcel.Range r3)
                range = r3;
        if (range == null) return null;
        return charactersObj != null ? new ExcelCharacters(charactersObj, range) : null;
    }
}